/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.delivery.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.OfflineHandleEventType;
import com.yami.shop.bean.model.OfflineHandleEvent;
import com.yami.shop.bean.model.Station;
import com.yami.shop.bean.param.OfflineHandleEventAuditParam;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.common.enums.SysTypeEnum;
import com.yami.shop.security.common.manager.TokenStore;
import com.yami.shop.security.platform.util.SecurityUtils;
import com.yami.shop.service.OfflineHandleEventService;
import com.yami.shop.service.StationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author YXF
 * @date 2020-04-23 15:18:29
 */
@RestController
@RequestMapping("/platform/station")
@Api(tags = "平台端自提接口")
public class StationController {

    @Autowired
    private StationService stationService;
    @Autowired
    private OfflineHandleEventService offlineHandleEventService;
    @Autowired
    private TokenStore tokenStore;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询自提点信息")
    public ServerResponseEntity<IPage<Station>> getStationPage(PageParam<Station> page, Station station) {
        return ServerResponseEntity.success(stationService.getPage(page, station));
    }

    @GetMapping("/info/{stationId}")
    @ApiOperation(value = "通过id查询自提点")
    @ApiImplicitParam(name = "stationId", value = "自提点id", required = true, dataType = "Long")
    public ServerResponseEntity<Station> getById(@PathVariable("stationId") Long stationId) {
        return ServerResponseEntity.success(stationService.getStationInfoById(stationId));
    }

    @PostMapping("/offline")
    @PreAuthorize("@pms.hasPermission('platform:station:update')")
    @ApiOperation(value = "下线自提点")
    public ServerResponseEntity<Void> offline(@RequestBody OfflineHandleEvent offlineHandleEvent) {
        Station station = stationService.getById(offlineHandleEvent.getHandleId());

        if (station == null) {
            // 未找到该自提点信息
            throw new YamiShopBindException("yami.station.no.find");
        }
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        stationService.offline(station, offlineHandleEvent.getOfflineReason(), sysUserId);
        // 移除缓存
        stationService.removeCacheByStationId(station.getStationId());
        tokenStore.deleteAllToken(SysTypeEnum.STATION.value().toString(),station.getStationId().toString());
        return ServerResponseEntity.success();
    }

    @GetMapping("/getOfflineHandleEventByStationId/{stationId}")
    @ApiOperation(value = "通过自提点id获取下线信息")
    @ApiImplicitParam(name = "stationId", value = "自提点id", required = true, dataType = "Long")
    public ServerResponseEntity<OfflineHandleEvent> getOfflineHandleEventByDiscountId(@PathVariable("stationId") Long stationId) {
        OfflineHandleEvent offlineHandleEvent = offlineHandleEventService.getProcessingEventByHandleTypeAndHandleId(OfflineHandleEventType.STATION.getValue(), stationId);
        return ServerResponseEntity.success(offlineHandleEvent);
    }

    @PostMapping("/auditStation")
    @PreAuthorize("@pms.hasPermission('platform:station:update')")
    @ApiOperation(value = "审核活动")
    public ServerResponseEntity<Void> auditSeckill(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        Station station = stationService.getById(offlineHandleEventAuditParam.getHandleId());
        if (station == null) {
            // 未找到该自提点信息
            throw new YamiShopBindException("yami.station.no.find");
        }
        stationService.auditSeckill(offlineHandleEventAuditParam, sysUserId);

        // 移除缓存
        stationService.removeCacheByStationId(station.getStationId());
        return ServerResponseEntity.success();
    }
}
